<?php

namespace App\Http\Controllers\Admin;
use App\Helpers\ResponseData;
use App\Http\Controllers\Controller;
use App\Http\Requests\Admin\UserRequest;
use App\Models\SystemUser;
use App\Models\User;
use Illuminate\Http\Request;
class UserController extends Controller
{
    use ResponseData;
    public function list(SystemUser $model,Request $request){
        $size = $request->input('size',10);
        $data = $model->with('department')->orderByDesc('id')->paginate($size);
        return $this->success($data);
    }
    public function store(SystemUser $user,UserRequest $request){
        $data = $request->only('department_id','name','phone');
        $user->query()->create($data);
        return $this->success('添加成功');
    }
    public function update(SystemUser $model,Request $request){
        $data = $model->find($request->id);
        if(!$data){
            return $this->failed('用户不存在',201);
        }
        $res = $request->all();
        $data->update($res);
        return $this->success('修改成功');
    }
    //编辑
    public function edit(SystemUser $model,Request $request){
        $data = $model->find($request->id);
        return $this->success($data);
    }
    //根据手机号查询用户
    public function getUserByPhone(SystemUser $model,Request $request){
        $user = $model->query()->where('phone',$request->phone)->first();
        return $this->success($user);
    }
}
